
import plugin from '../../../../lib/plugins/plugin.js';
import { getPlayerByE, pm } from './main.js';
import Rob from '../service/rob.js';
import GoShop from '../service/go_shop.js';
import RobotError from '../model/errors/robot-error.js';
import Shop from '../model/shop/shop.js';


export class LNERobot extends plugin {
    playerManager: import("d:/QQRobot/Yunzai/Miao-Yunzai/plugins/LNERobot/ts/model/player/player_manager").default;
    constructor() {
        super({
            name: '商店',
            dsc: '机器人',
            event: 'message',
            priority: 5,
            rule: [
                {
                    /** 命令正则匹配 */
                    reg: '^商店$',
                    /** 执行方法 */
                    fnc: 'shop'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^购买 .*( [0-9]+)?$',
                    /** 执行方法 */
                    fnc: 'buy'
                },
            ]
        });

        this.playerManager = pm;
    }

    async shop(e) {
        let shop = new Shop();
        await super.reply(shop.getGoodsStr());
    }

    async buy(e) {
        let player = await getPlayerByE(e);
        let shop = new GoShop(player);

        let msg: string = e.msg;
        let args = msg.split(' ');
        try {
            let rMsg = '';
            if (args.length < 3) rMsg = shop.buy(args[1]).msg;
            else rMsg = shop.buy(args[1], Number.parseInt(args[2])).msg;
            await this.playerManager.savePlayer(player);
            await super.reply(rMsg);
        } catch (err) {
            if (err instanceof RobotError) {
                await super.reply(err.message);
            }
        }
    }

}
